package cn.darkhorse.device.service.impl;

import cn.darkhorse.device.pojo.MesDeviceConsumable;
import cn.darkhorse.device.mapper.MesDeviceConsumableMapper;
import cn.darkhorse.device.service.MesDeviceConsumableService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 耗材管理 服务实现类
 * </p>
 *
 * @author yangtianqi
 * @since 2024-11-06
 */
@Service
public class MesDeviceConsumableServiceImpl extends ServiceImpl<MesDeviceConsumableMapper, MesDeviceConsumable> implements MesDeviceConsumableService {

    @Override
    public boolean saveMesDeviceConsumable(MesDeviceConsumable mesDeviceConsumable) {
        if (mesDeviceConsumable.getId()==null){

            Long count = lambdaQuery().eq(MesDeviceConsumable::getCode, mesDeviceConsumable.getCode()).count();
            if (count>0){
                throw new RuntimeException("编码不可重复");
            }
            mesDeviceConsumable.setCreateBy(SecurityUtils.getUsername());
            mesDeviceConsumable.setCreateTime(LocalDateTime.now());
        }else {
            Long count = lambdaQuery().eq(MesDeviceConsumable::getCode, mesDeviceConsumable.getCode()).ne(MesDeviceConsumable::getId,mesDeviceConsumable.getId()).count();
            if (count>0){
                throw new RuntimeException("编码不可重复");
            }
        }

        return saveOrUpdate(mesDeviceConsumable);
    }

    @Override
    public boolean delete(Long id) {
        return removeById(id);
    }

    @Override
    public MesDeviceConsumable view(Long id) {
        return getById(id);
    }

    @Override
    public List<MesDeviceConsumable> list(MesDeviceConsumable mesDeviceConsumable) {
        LambdaQueryWrapper<MesDeviceConsumable> queryWrapper = new LambdaQueryWrapper<>();
        if (mesDeviceConsumable.getCode()!=null){
            queryWrapper.like(MesDeviceConsumable::getCode,mesDeviceConsumable.getCode());
        }
        if (mesDeviceConsumable.getName()!=null){
            queryWrapper.like(MesDeviceConsumable::getName,mesDeviceConsumable.getName());
        }
        if (mesDeviceConsumable.getEnable()!=null){
            queryWrapper.like(MesDeviceConsumable::getEnable,mesDeviceConsumable.getEnable());
        }

        return list(queryWrapper);
    }
}
